package com.example.zlw.skindemo;

import android.content.Context;
import android.support.v4.view.LayoutInflaterFactory;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZLW on 2017/7/20.
 */

public class SkinFactory implements LayoutInflaterFactory {
    private static final String[] preStr = new String[]{
            "android.webkit.",
            "android.widget.",
            "android.view."
    };

    List<SkinView> skinCacheList = new ArrayList<>();

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        View view = null;
        if (name.contains(".")) {
            createView(context, name, attrs);
        } else {
            for (String pre :
                    preStr) {
                view = createView(context, pre + name, attrs);
                if (view != null) {
                    break;
                }

            }
        }

        if (view != null) {
            parseSkinView(context, attrs, view);
        }

        return view;
    }

    /**
     * 用于记录需要换肤的view
     * @param context
     * @param attrs
     * @param view
     */
    private void parseSkinView(Context context, AttributeSet attrs, View view) {
        List<SkinItem> list = new ArrayList<>();
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            String attrName = attrs.getAttributeName(i);
            String attrValue = attrs.getAttributeValue(i);

            if ("background".equalsIgnoreCase(attrName)
                    || "textColor".equalsIgnoreCase(attrName)
                    || "drawable".equalsIgnoreCase(attrName)) {
                int resId = Integer.parseInt(attrValue.substring(1));
                String attrValueType = context.getResources().getResourceTypeName(resId);
                String attrValueName = context.getResources().getResourceName(resId);
                SkinItem skinItem = new SkinItem(attrName, resId, attrValueType, attrValueName);

                list.add(skinItem);
            }

            if (!list.isEmpty()) {
                SkinView skinView = new SkinView(view, list);
                skinCacheList.add(skinView);

            }
        }
    }

    /**
     * 用于创建View
     *
     * @param context
     * @param name
     * @return
     */
    private View createView(Context context, String name, AttributeSet attrs) {

        try {
            Class<?> aClass = context.getClassLoader().loadClass(name);
            Constructor<? extends View> constructor =
                    (Constructor<? extends View>) aClass.getConstructor(
                            new Class[]{Context.class, AttributeSet.class});
            return constructor.newInstance(context, attrs);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    class SkinView {
        private View view;
        private List<SkinItem> list;

        public SkinView(View view, List<SkinItem> list) {
            this.view = view;
            this.list = list;
        }

        public void apply() {
            for (SkinItem skinItem : list) {
                if ("textColor".equalsIgnoreCase(skinItem.attrName)) {
                    if (view instanceof TextView) {
                        //颜色是插件化获取的
                        ((TextView) view).setTextColor(SkinManager.getOutInstance().getColor(skinItem.resId));
                    }
                } else if ("background".equalsIgnoreCase(skinItem.attrName)) {
                    if ("color".equalsIgnoreCase(skinItem.attrValueType)) {
                        view.setBackgroundColor(SkinManager.getOutInstance().getColor(skinItem.resId));
                    }
                    else if ("drawable".equalsIgnoreCase(skinItem.attrValueType)) {
                        view.setBackground(SkinManager.getOutInstance().getDrawable(skinItem.resId));
                    }
                }

            }
        }
    }

    class SkinItem {
        //android:background="@color/colorPrimary"
        //android:background="@drawable/colorPrimary"
        //android:background="@mipmap/colorPrimary"

        String attrName;

        int resId;

        public SkinItem(String attrName, int resId,
                        String attrValueType, String attrValueName) {
            this.attrName = attrName;
            this.resId = resId;
            this.attrValueType = attrValueType;
            this.attrValueName = attrValueName;
        }

        String attrValueType;

        String attrValueName;
    }

    public void apply() {
        for (SkinView skinView : skinCacheList) {
            skinView.apply();
        }
    }
}
